package com.jn.okhttp.presenter;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.jn.okhttp.common.WeakRrfHandler;
import com.jn.okhttp.contract.HotsearchContact;
import com.jn.okhttp.entity.HotSearchEntity;
import com.jn.okhttp.utils.LogUtils;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class HotSearchPresenter implements HotsearchContact.IHotSearchPresenter {
    public static final int HTTP_SUCCESS = 0;
    public static final int HTTP_FAIL = 1;

    private HotsearchContact.IHotView mMainView;
    private Context mContext;
    private OkHttpClient mClient;

    public HotSearchPresenter(HotsearchContact.IHotView mMainView, Context mContext) {
        if (mMainView == null) {
            throw new IllegalArgumentException("mMainView cannot be null");
        }
        this.mMainView = mMainView;
        this.mContext = mContext;
        mClient = new OkHttpClient();
    }

    @Override
    public void getData(String type) {
        LogUtils.d("HotSearchPresenter", "type == " + type);
        String url = "https://api.oioweb.cn/api/common/fetchHotSearchBoard?type=" + type;

        Request request = new Request.Builder()
                .url(url)
                .get()
                .build();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Response response = mClient.newCall(request).execute();
                    if (response.isSuccessful()) {
                        String body = response.body().string();
                        LogUtils.d("HotSearchPresenter", "onResponse : " + body);
                        Message message = Message.obtain();
                        message.obj = body;
                        message.what = HTTP_SUCCESS;
                        mHandler.sendMessage(message);
                    } else {
                        // 处理请求失败的情况
                        Message message = Message.obtain();
                        message.what = HTTP_FAIL;
                        mHandler.sendMessage(message);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    Message message = Message.obtain();
                    message.what = HTTP_FAIL;
                    mHandler.sendMessage(message);
                }
            }
        }).start();
    }

    WeakRrfHandler<HotSearchPresenter> mHandler = new WeakRrfHandler<>(Looper.getMainLooper(), this, new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case HTTP_SUCCESS:
                    String body = (String) msg.obj;
                    Gson gson = new Gson();
                    HotSearchEntity searchEntity = gson.fromJson(body, HotSearchEntity.class);
                    if (mMainView != null) {
                        mMainView.showData(searchEntity);
                    }
                    break;
                case HTTP_FAIL:
                    if (mMainView != null) {
                        mMainView.showError("请求失败");
                    }
                    break;
            }
            return true;
        }
    });
}